﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
using Newtonsoft.Json.Linq;

namespace NetCoreCMS.Web.Areas.Admin.Controllers.Users
{
    public class User_AuditController : ManageController
    {
        private const string VIEWURL = "~/Areas/Admin/Views/Users/User_Audit.cshtml";
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected string keywords = string.Empty;
        public IUsersService _users { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_audit", CMSEnums.ActionEnum.View.ToString()); //检查权限
            this.keywords = Request.Query["keywords"];
            int.TryParse(Request.Query["page"], out this.page);
            int.TryParse(Request.Query["pagesize"], out this.pageSize);
            if (this.pageSize > 0)
            {
                Response.Cookies.Append("user_audit_page_size", this.pageSize.ToString());
            }
            else
            {
                this.pageSize = GetPageSize(10); //每页数量
            }

            ViewBag.Keywords = this.keywords;
            ViewBag.PageSize = this.pageSize.ToString();
        }

        //
        // GET: /admin/User_Audit/
        public ActionResult Index()
        {
            RptBind("where status>0 and status<3" + CombSqlTxt(this.keywords), "reg_time desc,id desc");
            return View(VIEWURL);
        }

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            int.TryParse(Request.Query["page"], out this.page);
            this.page = this.page <= 0 ? 1 : this.page;
            ViewData["list"] = _users.GetList(this.page, this.pageSize, _strWhere, _orderby, null);
            this.totalCount = _users.RecordCount(_strWhere);
            ViewBag.PageContent = Utils.OutPageList(this.pageSize, this.page, this.totalCount, Utils.CombUrlTxt("index", "keywords={0}&page={1}", this.keywords, "__id__"), 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder strTemp = new StringBuilder();            
            if (!string.IsNullOrEmpty(_keywords))
            {
                _keywords = _keywords.Replace("'", "");
                strTemp.Append(" and (user_name like '%" + _keywords + "%' or mobile like '%" + _keywords + "%' or email like '%" + _keywords + "%' or nick_name like '%" + _keywords + "%')");
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回用户每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Request.Cookies["user_audit_page_size"], out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        //审核通过
        [HttpPost]
        public ActionResult SubmitAudit()
        {
            ChkAdminLevel("user_audit", CMSEnums.ActionEnum.Audit.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            //批量删除
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken record = jobject["list"];
            int id;
            foreach (JToken item in record)
            {
                if (int.TryParse(item["id"].ToString(), out id))
                {
                    if (_users.UpdateField(id, "status=0") > 0)
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(CMSEnums.ActionEnum.Audit.ToString(), "审核会员成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            string result=JscriptMsg("审核通过" + sucCount + "条，失败" + errorCount + "条！", Utils.CombUrlTxt("index", "keywords={0}", this.keywords));
            return Content(result);
        }

        //delete
        [HttpPost]
        public ActionResult SubmitDelete()
        {
            ChkAdminLevel("user_list", CMSEnums.ActionEnum.Audit.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            //批量删除
            JObject jobject = JObject.Parse(Request.Form["json"]);
            JToken record = jobject["list"];
            int id;
            foreach (JToken item in record)
            {
                if (int.TryParse(item["id"].ToString(), out id))
                {
                    if (_users.Delete(id))
                    {
                        sucCount += 1;
                    }
                    else
                    {
                        errorCount += 1;
                    }
                }
            }
            AddAdminLog(CMSEnums.ActionEnum.Delete.ToString(), "删除用户" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            string result=JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("index", "keywords={0}", this.keywords)); 
            return Content(result);
        }
    }
}
